package com.nowcoder.community.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MyClassLoader extends ClassLoader{
    private File classPathFile;
    private static Map<String, Class> clazzCache = new ConcurrentHashMap<>();
    public MyClassLoader(){
        String classPath = MyClassLoader.class.getResource("").getPath();
        this.classPathFile = new File(classPath);
    }
    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException{
        return this.findClass(name, false);
    }
    protected Class<?> findClass(String name, boolean force) throws ClassNotFoundException{
        Class cls = clazzCache.get(name);
        if(force){
            cls = null;
        }
        String className = MyClassLoader.class.getPackageName() +'.'+name;
        if(cls == null && classPathFile !=null){
            File classFile = new File(classPathFile + "\\" + name.replaceAll("\\.","/") + ".class");
            if(classFile.exists()){
                FileInputStream fis = null;

                ByteArrayInputStream bos = null;
                try {
                    fis = new FileInputStream(classFile);
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                }

            }
        }
        return null;
    }

}
